﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.CottagesCommands
{
    public class CottageDetailHander : IRequestHandler<CottageDetailCommand, ResultT<Cottages>>
    {

        private readonly ILogger<CottageDetailHander> logger;
        private readonly IBaseRepository<Cottages> cottageRepository;

        public CottageDetailHander(ILogger<CottageDetailHander> logger, IBaseRepository<Cottages> cottageRepository)
        {
            this.logger = logger;
            this.cottageRepository = cottageRepository;
        }

        /// <summary>
        /// 反添栋舍信息方法
        /// </summary>
        /// <param name="request">反添栋舍信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>反添栋舍信息响应对象</returns>
        public async Task<ResultT<Cottages>> Handle(CottageDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultT<Cottages>();

                result.Item =  cottageRepository.GetAll().FirstOrDefault(c => c.CottagesId == request.CottagesId);
                logger.LogInformation("反添栋舍信息方法调用成功");
                return result;

            }
            catch (Exception ex)
            {
                logger.LogError("反添栋舍信息方法出错" + ex);
                throw;
            }
        }
    }
}
